import {dirname,join} from 'path'
import {mkdirSync,existsSync,readFileSync,writeFileSync} from 'fs'
import {ROOT} from './dir.coffee'

config = join(ROOT,'conf')

rp = (p)=>
  fp = join(config,p)
  mkdirSync dirname(fp), {recursive:true}
  fp

export default new Proxy(
  {}
  {
    set:(self, prop, value)->
      writeFileSync(
        rp prop
        JSON.stringify value
      )
      value

    get:(self, prop)=>
      filepath = rp prop
      if existsSync(filepath)
        JSON.parse readFileSync(filepath)
      else
        undefined
  }
)

